package com.shrct.businessprocess.product;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.product.RCAnonymousProductEntry;
import com.shrct.businessprocess.product.RCAnonymousProductPurchaseInfo;
import com.shrct.businessprocess.product.RCAnonymouseProductEntryRoot;
import com.shrct.businessprocess.product.events.RCAnonymouseProductEntryRootEventsListener;
import com.shrct.businessprocess.product.impl.RCAnonymousProductPurchaseInfoImpl;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.event.BaseEntityEventsListener;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Date;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.product.RCProductPurchaseInfoBuilder4AnonymouseProduct")
public class RCProductPurchaseInfoBuilder4AnonymouseProduct extends BaseEntityEventsListener<RCAnonymouseProductEntryRoot> implements RCAnonymouseProductEntryRootEventsListener {
  public void _inStock(final RCAnonymouseProductEntryRoot entity, final RCPurchaseOrderItem orderItem, final Integer quantity) {
    IGenericQuery<RCAnonymousProductPurchaseInfo> query=getObjectFactory().createGenericQuery(RCAnonymousProductPurchaseInfo.class,"select e from com.shrct.businessprocess.product.impl.RCAnonymousProductPurchaseInfoImpl as e   where ((e.entryRoot) = (:entity)) and  e.active = 1      ").setParameter("entity",entity);
    RCAnonymousProductPurchaseInfo purchaseInfo = query.uniqueResult();
    boolean _equals = Objects.equal(purchaseInfo, null);
    if (_equals) {RCAnonymousProductPurchaseInfo rCAnonymousProductPurchaseInfo=RCAnonymousProductPurchaseInfoImpl.create(entity,getObjectFactory());
      final Procedure1<RCAnonymousProductPurchaseInfo> _function = new Procedure1<RCAnonymousProductPurchaseInfo>() {
          public void apply(final RCAnonymousProductPurchaseInfo it) {
            it.setOrderItem(orderItem);
            Date _day = DateUtil.today();
            it.setInStockDate(_day);
            it.setInStockQuantity(quantity);
          }
        };
      RCAnonymousProductPurchaseInfo _doubleArrow = ObjectExtensions.<RCAnonymousProductPurchaseInfo>operator_doubleArrow(rCAnonymousProductPurchaseInfo, _function);
      purchaseInfo = _doubleArrow;
    } else {
      Integer _inStockQuantity = purchaseInfo.getInStockQuantity();
      int _plus = ((_inStockQuantity).intValue() + (quantity).intValue());
      purchaseInfo.setInStockQuantity(Integer.valueOf(_plus));
    }
  }
  
  public void _deliver(final RCAnonymouseProductEntryRoot entity, final RCAnonymousProductEntry target, final Integer quantity) {
    IGenericQuery<RCAnonymousProductPurchaseInfo> query=getObjectFactory().createGenericQuery(RCAnonymousProductPurchaseInfo.class,"select e from com.shrct.businessprocess.product.impl.RCAnonymousProductPurchaseInfoImpl as e   where ((e.entryRoot) = (:entity)) and  e.active = 1      ").setParameter("entity",entity);
    RCAnonymousProductPurchaseInfo _uniqueResult = query.uniqueResult();
    final Procedure1<RCAnonymousProductPurchaseInfo> _function = new Procedure1<RCAnonymousProductPurchaseInfo>() {
        public void apply(final RCAnonymousProductPurchaseInfo it) {
          Date _day = DateUtil.today();
          it.setDeliverDate(_day);
          Integer _deliverQuantity = it.getDeliverQuantity();
          int _plus = ((_deliverQuantity).intValue() + (quantity).intValue());
          it.setDeliverQuantity(Integer.valueOf(_plus));
          RCEnterprise _enterprise = target.getEnterprise();
          it.addDeliverTarget(quantity, _enterprise);
        }
      };
    ObjectExtensions.<RCAnonymousProductPurchaseInfo>operator_doubleArrow(_uniqueResult, _function);
  }
  
  public void _return(final RCAnonymouseProductEntryRoot entity, final RCAnonymousProductEntry source, final Integer quantity) {
    IGenericQuery<RCAnonymousProductPurchaseInfo> query=getObjectFactory().createGenericQuery(RCAnonymousProductPurchaseInfo.class,"select e from com.shrct.businessprocess.product.impl.RCAnonymousProductPurchaseInfoImpl as e   where ((e.entryRoot) = (:entity)) and  e.active = 1      ").setParameter("entity",entity);
    RCAnonymousProductPurchaseInfo _uniqueResult = query.uniqueResult();
    final Procedure1<RCAnonymousProductPurchaseInfo> _function = new Procedure1<RCAnonymousProductPurchaseInfo>() {
        public void apply(final RCAnonymousProductPurchaseInfo it) {
          Integer _deliverQuantity = it.getDeliverQuantity();
          int _minus = ((_deliverQuantity).intValue() - (quantity).intValue());
          it.setDeliverQuantity(Integer.valueOf(_minus));
        }
      };
    ObjectExtensions.<RCAnonymousProductPurchaseInfo>operator_doubleArrow(_uniqueResult, _function);
  }
}
